package com.qiangqiang.controller;

import com.qiangqiang.dto.StudentDTO;
import com.qiangqiang.entity.Student;
import com.qiangqiang.service.StudentService;
import com.qiangqiang.util.ResultVOUtil;
import com.qiangqiang.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by q on 2018/2/19.
 */
@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/update")
    public ResultVO<Student> addOrUpdateStudent(Student student) {
        return ResultVOUtil.success(studentService.addOrUpdateStudent(student));
    }

    @PostMapping("/delete")
    public ResultVO deleteStudent(@RequestParam("id") String id) {
        studentService.deleteStudent(id);
        return ResultVOUtil.success();
    }

    @PostMapping("/findAll")
    public ResultVO<Page<Student>> findAllStudents(@RequestParam(value = "page", defaultValue = "0") int page,
                                                   @RequestParam(value = "size",defaultValue = "10") int size) {
        return ResultVOUtil.success(studentService.findAllStudents(page,size));
    }

    @PostMapping("/findBySid")
    public ResultVO<StudentDTO> findById(@RequestParam("id") String sId){
        return ResultVOUtil.success(studentService.findById(sId));
    }

    @PostMapping("findByDid")
    public ResultVO<List<Student>> findByDid(@RequestParam("id") String dId){
        return ResultVOUtil.success(studentService.findByDid(dId));
    }
}
